import { defineStore } from 'pinia'

export const useDebugStore = defineStore('debug', {
  state: () => ({
    key: 'LOG',
    list: []
  }),

  actions: {
    clean() {
      this.list = []
      uni.setStorageSync(this.key, JSON.stringify(this.list))
    },

    add(label, value) {
      this.update()
      this.list.push({
        label,
        value,
        type: typeof value,
        time: this.getCurrentTimeString()
      })
      uni.setStorageSync(this.key, JSON.stringify(this.list))
    },

    update() {
      const str = uni.getStorageSync(this.key) || ''
      if (str) {
        this.list = JSON.parse(str)
      } else {
        this.list = []
      }
    },

    getCurrentTimeString() {
      const currentDate = new Date()
      
      const month = String(currentDate.getMonth() + 1).padStart(2, '0')
      const date = String(currentDate.getDate()).padStart(2, '0')
      const hours = String(currentDate.getHours()).padStart(2, '0')
      const minutes = String(currentDate.getMinutes()).padStart(2, '0')
      const seconds = String(currentDate.getSeconds()).padStart(2, '0')
      
      return `${month}/${date} ${hours}:${minutes}:${seconds}`
    }
  }
})